//
//  ArticleHeaderCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/18.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ArticleHeaderCell.h"

@implementation ArticleHeaderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    self.img.backgroundColor = [UIColor colorWithHexString:@"E9E9F1"];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.img];
    self.img.layer.masksToBounds = YES;
    
    self.addbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addbutton.frame = CGRectMake(0, 100 - 50, ScreenWidth, 100);
    [self.addbutton setTitle:@"添加封面" forState:UIControlStateNormal];
    self.addbutton.titleLabel.font = [UIFont systemFontOfSize:20];
    self.addbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.contentView addSubview:self.addbutton];
    [self.addbutton addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
    
    self.title =  [[UITextField alloc] initWithFrame:CGRectMake(12, 200, ScreenWidth - 24, 50)];
    _title.placeholder =@"输入文字标题";
    _title.textAlignment = NSTextAlignmentLeft;
    _title.font = [UIFont systemFontOfSize:15];
    _title.delegate = self;
    [self.contentView addSubview:self.title];
    
    self.desc =  [[UITextField alloc] initWithFrame:CGRectMake(12, 250, ScreenWidth - 24, 50)];
    _desc.placeholder =@"输入描述";
    _desc.textAlignment = NSTextAlignmentLeft;
    _desc.font = [UIFont systemFontOfSize:15];
    _desc.delegate = self;
    [self.contentView addSubview:self.desc];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(12, 250, ScreenWidth - 24, 1)];
    line1.backgroundColor = LineColor;
    [self.contentView addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(12, 300, ScreenWidth - 24, 1)];
    line2.backgroundColor = LineColor;
    [self.contentView addSubview:line2];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [PublishData sharedPublishData].pubtitle = self.title.text;
    [PublishData sharedPublishData].pubdesc = self.desc.text;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)noticeDelegate {
    if (self.delegate && [self.delegate respondsToSelector:@selector(articleHeaderChooseOne:)]) {
        [self.delegate articleHeaderChooseOne:self.img.image];
    }
}

- (void)clickAdd {
    UIAlertController* alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction* takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }];
    
    UIAlertAction* choosePictureAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self choosePhoto];
    }];
    
    [alertVC addAction:cancleAction];
    [alertVC addAction:takePhotoAction];
    [alertVC addAction:choosePictureAction];
    
    if (self.delegate) {
        UIViewController *vc = (UIViewController *)self.delegate;
        [vc presentViewController:alertVC animated:YES completion:nil];
    }
}

- (void)openCamera {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    
    if (self.delegate) {
        UIViewController *vc = (UIViewController *)self.delegate;
        [vc presentViewController:picker animated:YES completion:^{
        }];
    }
}

- (void)choosePhoto {
    NSInteger maxCount = 1;
    if (ISIOS7 || ISIOS8_0) {
        ELCImagePickerController *elcPicker = [ELCImagePickerController new];
        elcPicker.maximumImagesCount = maxCount;
        elcPicker.imagePickerDelegate = self;
        
        if (self.delegate) {
            UIViewController *vc = (UIViewController *)self.delegate;
            [vc presentViewController:elcPicker animated:YES completion:nil];
        }
    } else {
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.assetCollectionSubtypes =@[
                                                         @(PHAssetCollectionSubtypeSmartAlbumUserLibrary), //相机胶卷
                                                         @(PHAssetCollectionSubtypeAlbumMyPhotoStream), //我的照片流
                                                         @(PHAssetCollectionSubtypeSmartAlbumPanoramas), //全景图
                                                         @(PHAssetCollectionSubtypeSmartAlbumBursts), //连拍模式拍摄的照片
                                                         @(PHAssetCollectionSubtypeSmartAlbumFavorites)
                                                         ];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.mediaType = QBImagePickerMediaTypeImage;
        imagePickerController.maximumNumberOfSelection = maxCount;
        imagePickerController.showsNumberOfSelectedAssets = YES;
        if (self.delegate) {
            UIViewController *vc = (UIViewController *)self.delegate;
            [vc presentViewController:imagePickerController animated:YES completion:nil];
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    [picker dismissViewControllerAnimated:YES completion:^{
        self.img.image = image;
        [self noticeDelegate];
    }];
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    //TODO:changeToData
    [picker dismissViewControllerAnimated:YES completion:^{
        for (int i = 0;i<[info count];i++) {
            UIImage *img = [[info objectAtIndex:i]objectForKey:@"UIImagePickerControllerOriginalImage"];
            self.img.image = img;
            [self noticeDelegate];
        }
    }];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        DYLog(@"cancl");
    }];
}

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
    NSUInteger count = assets.count;
    __block NSUInteger select = 0;
    PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
    option.synchronous = YES;
    for (PHAsset *asset in assets) {
        [[PHCachingImageManager defaultManager]requestImageDataForAsset:asset options:option resultHandler:^(NSData * imageData, NSString * dataUTI, UIImageOrientation orientation, NSDictionary * info) {
            if(imageData) {
                self.img.image = [UIImage imageWithData:imageData];
                [self noticeDelegate];
                select++;
            }
            if(select == count) {
                [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
                return;
            }
        }];
        
    }
}

- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [imagePickerController dismissViewControllerAnimated:NO completion:NULL];
}

@end
